<template>
  <div class="comment-border">
    <div class="comment-bgc" v-if="!isLogin">
      <div class="comment-modal">
      </div>
      <div class="modal-text">
        <p style="margin-bottom: 5px;">请先登录！</p>
        <p>查看更多精彩视频</p>
      </div>
    </div>
    <svg width="36" height="36" fill="none" xmlns="http://www.w3.org/2000/svg" class="Rg2nrszA" viewBox="0 0 36 36"
      @click="changeShow('commentStatus')">
      <path
        d="M22.133 23.776a1.342 1.342 0 101.898-1.898l-4.112-4.113 4.112-4.112a1.342 1.342 0 00-1.898-1.898l-4.112 4.112-4.113-4.112a1.342 1.342 0 10-1.898 1.898l4.113 4.112-4.113 4.113a1.342 1.342 0 001.898 1.898l4.113-4.113 4.112 4.113z"
        fill="#ffffff"></path>
    </svg>
    <div :class="['section-box', 'section-box-active']">评论</div>
    <div class="comment-total">全部评论（{{ commentList.length }}）</div>
    <div :class="{
      'comment-insidebox': true,
      'filterActive': !isLogin
    }">
      <comment-info v-for="(item, index) in commentList" :key="index" :comment="getComment(item)"></comment-info>
    </div>

    <!-- 添加评论输入区域 -->
    <div class="comment-input-area">
      <div class="input-wrapper">
        <el-input v-model="commentText" type="textarea" :rows="3" placeholder="说点什么..." resize="none" maxlength="200"
          show-word-limit :disabled="!isLogin" />
      </div>
      <div class="button-wrapper">
        <el-button type="primary" :disabled="!commentText.trim() || !isLogin" @click="sendComment">
          发送评论
        </el-button>
      </div>
    </div>
  </div>
</template>

<script>
import CommentInfo from './components/commentInfo.vue'
import { mapMutations } from 'vuex'
import { ref } from 'vue'
import { ElMessage } from 'element-plus'

export default {
  components: { CommentInfo },
  props: {
    commentList: {
      type: Array,
      default() {
        return []
      },
    }
  },
  setup(props, { emit }) {
    const commentText = ref('')

    const sendComment = async () => {
      if (!commentText.value.trim()) {
        ElMessage.warning('评论内容不能为空')
        return
      }

      try {
        emit('send-comment', commentText.value)
        commentText.value = '' // 清空输入框
      } catch (error) {
        console.error('发送评论失败:', error)
        ElMessage.error('发送评论失败')
      }
    }

    return {
      commentText,
      sendComment
    }
  },
  computed: {
    isLogin: {
      get() { return this.$store.state.user.isLogin }
    },
    commentStatus: {
      get() { return this.$store.state.videoFunctionList.commentStatus }
    }
  },
  methods: {
    ...mapMutations('videoFunctionList', { changeShow: 'CHANGE_STATUS' }),
    getComment(val) {
      return {
        ...val,
        name: val.comment_userName,
        info: val.comment_info,
        time: val.comment_time
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.comment-border {
  position: relative;
  box-sizing: border-box;
  padding: 0 15px;
  width: 40%;
  height: 100%;
  background-color: rgb(36, 36, 41);
  overflow: hidden;
}
.comment-bgc {
  position: absolute;
  z-index: 1000;
  width: 100%;
  height: 100%;
  margin-left: -15px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}
.comment-modal {
  height: 183px;
  width: 183px;
  border-radius: 15px;
  background: url(@/assets/img/pleaseLogin.jpg) 0% 0% / cover no-repeat;
  overflow: hidden;
}
.modal-text {
  margin-top: 20px;
  color: rgb(228, 229, 229);
  font-size: 14px;
  text-align: center;
}
.comment-border svg {
  position: relative;
  z-index:2000;
  float: right; 
  margin-top: 10px;
  cursor: pointer;
}
.section-box {
  display: flex;
  width: 38px;
  height: 53px;
  line-height: 53px;
  font-size: 18px;
  font-weight: 500;
  color: rgb(255, 255, 255);
  border-bottom: 3px solid;
}
.section-box-active {
  border-bottom-color: rgb(255, 44, 85)
}
.comment-total {
  height: 21px;
  line-height: 21px;
  margin-top: 10px;
  font-weight: 400;
  font-size: 16px;
  color: white;
}
.section-box-info {
  display: flex;
  flex-direction: column;
}
.comment-insidebox {
  margin-top: 10px;
  display: flex;
  flex-direction: column;
  width: 100%;
  height: calc(100% - 56px - 31px - 180px); /* 减去输入框的高度 */
  overflow: auto;
}
.filterActive {
  filter: blur(5px);
}
/* 滚动条整体样式 */
.comment-insidebox::-webkit-scrollbar {
  width: 5px;
  height: 5px;
}

.comment-insidebox::-webkit-scrollbar-thumb {
  background: linear-gradient(to bottom right, rgba(255, 255, 255, 0.6) 0%, rgba(255, 255, 255, 0.6) 100%);
  border-radius: 5px;
}

.comment-insidebox::-webkit-scrollbar-track {
  background-color: transparent;
}

/* 向下箭头按钮样式 */
.comment-insidebox::-webkit-scrollbar-button:start:decrement {
  width: 5px;
  height: 5px;
  border-radius: 2.5px;
  background-color: #fff;
}

/* 向上箭头按钮样式 */
.comment-insidebox::-webkit-scrollbar-button:end:increment {
  width: 5px;
  height: 5px;
  border-radius: 2.5px;
  background-color: #fff;
}

.comment-insidebox::-webkit-scrollbar-button:hover {
  cursor: pointer;
}

/* 添加评论输入区域样式 */
.comment-input-area {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 20px 15px;
  background: rgb(36, 36, 41);
  border-top: 1px solid rgba(255, 255, 255, 0.1);
}

.input-wrapper {
  margin-bottom: 15px;
}

:deep(.el-textarea) {
  .el-textarea__inner {
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid rgba(255, 255, 255, 0.1);
    color: #fff;
    resize: none;

    &:focus {
      border-color: rgb(255, 44, 85);
    }

    &::placeholder {
      color: rgba(255, 255, 255, 0.5);
    }
  }

  .el-input__count {
    background: transparent;
    color: rgba(255, 255, 255, 0.5);
  }
}

.button-wrapper {
  display: flex;
  justify-content: flex-end;

  .el-button {
    background: rgb(255, 44, 85);
    border-color: rgb(255, 44, 85);
    padding: 8px 24px;
    
    &:not(:disabled):hover {
      background: rgba(255, 44, 85, 0.8);
      border-color: rgba(255, 44, 85, 0.8);
    }

    &:disabled {
      background: rgba(255, 255, 255, 0.1);
      border-color: transparent;
    }
  }
}
</style>